import sys
import optparse

from abc import ABCMeta, abstractmethod
from utils.OptionParserUtil import *

"""
@author: Ritesh Agrawal
@version: 1.0
@since: 03/11/11
@summary: Base class. Any class should be an implementation of this class 
"""   

class EasyHadoop():
    
    __metaclass__ = ABCMeta
    
    parser = OptionParser()
    options = None   
    arguments = None
    mandatory = []

    def __init__(self, arguments):

        self.parser = OptionParser(
                    usage=self.get_usage(),    
                    description=self.get_description()
                )
        self.set_arguments(arguments)
        self.set_mandatory()
        self.set_commands()
        self.validate()
    
    def set_arguments(self, arguments):
        if(arguments == None): return
        
        if (isinstance(arguments,str)):
            self.arguments = arguments.split()
        elif (isinstance(arguments, (tuple, list))):
            self.arguments = arguments
        return
            
    def set_mandatory(self):
        '''set mandatory options'''
        self.mandatory.append('--input-delimiter')
        self.mandatory.append('--output-delimiter')
        self.mandatory.append('--debug')
    
    def set_commands(self):
        '''set options'''
        self.parser.add_option("--debug", action="store_true", dest="debug", 
                               help="Print debug statements", default=False)
        self.parser.add_option("--no-ignore", action="store_false", dest="ignore",
                               default="true", help="Raise exception")
        
        self.parser.add_option("--input-delimiter", 
                               action="callback", callback=handle_delimiter, 
                               dest="input_delimiter", type="string", 
                               help="Input delimiter", default="\t", 
                               nargs=1)
        #TODO: handle tab option for output-delimiter
        self.parser.add_option("--output-delimiter", 
                               action="callback", callback=handle_delimiter,  
                               dest="output_delimiter", type="string", 
                               help="Output delimiter", default="\t", 
                               nargs=1)

    def validate(self):
        '''validate arguments'''
        (self.options, self.arguments) = self.parser.parse_args(args=self.arguments)

        missing = []
        for key in self.mandatory:
            if not self.parser.exists(key): missing.append(key)
                
        if len(missing) > 0: 
            print("Error: Missing required options %s" % ", ".join(missing))
            self.parser.print_help()
            exit(-1)
    
        if(self.options.debug): print("%s" % self.options)
        
    @abstractmethod
    def get_description(self):
        pass
    
    def get_usage(self):
        return "Usage: python %prog [options]"
    
    @abstractmethod
    def execute(self, line):
        pass
    
if __name__ == '__main__':
    cls = EasyHadoop(sys.argv)
    
